<?php

namespace App\Models\Ddaman;

use App\Models\xwbei\Student;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Models\common\Base;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

class WorkflowPersonnel extends Base
{
    use HasFactory;

    protected $table = "workflow_personnel";

    //const CREATED_AT = null;
    //const UPDATED_AT = null;

    const CHANGE_STATUS = 1;

    protected $fillable = [
        'wid','uid','change_type','change_reason','applicant','student_id','company_id','company_name'
    ];


    public $rule = [
        'name' => 'required|string|between:2,100',
        'logo' => 'required',
    ];

    public $editRule = [
        'id' => 'required',
        'name' => 'required|string|between:2,100',
        'logo' => 'required',
    ];

    public $message = [
        'id.required' => '标识必须',
        'name.required' => '分类名称必须填写',
        'logo.required'=>"请上传品牌logo",
    ];

    /*
     * 获取列表
     * */
    public function getList( $param = [] )
    {
        $limit = isset($param['limit']) ? $param['limit'] : 10;
        $where = [];

        if (isset($param['keywords']) && $param['keywords']){
            $keywords = trim($param['keywords']);
            $where[] = ['name','like',"%$keywords%"];
        }

        $field = array_merge(['id'],$this->fillable);
        $list = $this->where($where)->orderBy('id','desc')->select($field)->paginate($limit);

        return $list;

    }

    /*
     * 人事变动
     * */
    public function changes( $data )
    {
        $user = get_user();
        $data['uid'] = $user->guid;
        $data['applicant'] = $user->name;
        $data = stayFill($this->fillable,$data);
        $info = $this->where('wid',$data['wid'])->first();
        if ($info){
            $result = $this->where('id',$info->id)->update($data);
        }else{
            $result = $this->create($data);
        }
        if (!$result){
            $this->error = "更新失败";
            return false;
        }

        return true;
    }



    /*
     * 入职成功 回调
     * */
    public function successFlow( $wid )
    {

        $info = $this->where('wid',$wid)->first();
        if (!$info){
            $this->error = "信息有误";
            return false;
        }

        $status = null;
        $statusTitle = "";
        switch ($info->change_type)
        {
            case 1: //转正
                $status = 2;
                $statusTitle = "转正";
                break;
            case 2: //调岗
                $status = 4;
                $statusTitle = "调岗";
                break;
            case 3: //申请离职
                $status = 3;
                $statusTitle = "待离职";
                break;
            case 4: //正式离职
                $status = 4;
                $statusTitle = "正式离职";
                break;

        }

        if ($status){
            //学生 人事变动
            $companyStudent = EnterpriseStudent::where([
                ['company_id',$info->company_id],
                ['student_id',$info->student_id],
            ])->first();
            if ($companyStudent){
                $sData = [
                    'student_status'=>$status
                ];
                //修改学生状态 学生离职或者换岗
                $changeStatus = [3,4];
                if (in_array($status,$changeStatus)){
                    $sData['quit_date'] = $info->change_date;
                    $sData['quit_reason'] = $info->change_reason;
                }
                $result = EnterpriseStudent::where('id',$companyStudent->id)->update($sData);
                if (!$result){
                    $this->error = "E1001";
                    return false;
                }
            }

            //修改学生状态 学生离职或者换岗
            $studentStatus = [2,4];
            if (in_array($status,$studentStatus)){
                $student = Student::where('guid',$info->student_id)->select('id','guid','status')->first();
                switch ($status){
                    case 2:
                        $student->status = 3; //已转正
                        break;
                    case 4: //正式离职
                        $student->status = 0; //待实习
                        $student->company_id = NULL; //待实习
                        break;
                }

                if (!$student->save()){
                    $this->error = "E1003";
                    return false;
                }
            }

            //记录
            $result = EnterpriseStudentLog::create([
                'type'=>2,
                'company_id'=>$info->company_id,
                'student_id'=>$info->student_id,
                'tip'=> "申请 $statusTitle ",
            ]);
            if (!$result){
                $this->error = "E1005";
                return false;
            }
        }

        //dump($student);die;
        return true;
    }

    /*
     * 申请转正
     * */

    /*
     * 申请离职
     * */


}
